package com.d2rv.settings;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * Describes the JDomParser that update the settings of the application
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class JDomParser {

    static org.jdom2.Document document;
    static Element racine;

    /**
     * Default constructor.
     * @param fichier the path of the file to load
     * @throws Exception
     */
    public JDomParser(String fichier) throws Exception {
        SAXBuilder sxb = new SAXBuilder();
        document = sxb.build(new File(fichier));
        racine = document.getRootElement();
    }

    /**
     * Returns the value of the considered setting
     * @param classe indicates the class of the wanted value
     * @param att indicates the attribute of the wanted value
     * @return the value of the considered setting
     */
    public String getElement(String classe, String att) {
        List list = racine.getChildren(classe);
        Iterator listIterator = list.iterator();
        List listTmp;
        Iterator listTmpIterator;

        while (listIterator.hasNext()) {
            Element current = (Element) listIterator.next();
            listTmp = current.getChildren();
            listTmpIterator = listTmp.iterator();
            while (listTmpIterator.hasNext()) {
                Element currentTmp = (Element) listTmpIterator.next();
                if (currentTmp.getName().equals(att)) {
                    return currentTmp.getValue();
                }
            }
        }
        return classe + " " + att;

    }

    /**
     * Sets the value of the considered setting
     * @param classe indicates the class of the wanted value
     * @param att indicates the attribute of the wanted value
     * @param value the value to set
     */
    public void setElement(String classe, String att, float value) {
        List list = racine.getChildren(classe);
        Iterator listIterator = list.iterator();
        List listTmp;
        Iterator listTmpIterator;

        while (listIterator.hasNext()) {
            Element current = (Element) listIterator.next();
            listTmp = current.getChildren();
            listTmpIterator = listTmp.iterator();
            while (listTmpIterator.hasNext()) {
                Element currentTmp = (Element) listTmpIterator.next();
                if (currentTmp.getName().equals(att)) {
                    current.setAttribute(att, Float.toString(value));
                }
            }
        }
    }

        /**
     * Save the file  
     * @param fichier the path of the file to save

     */
    void saveFile(String fichier) throws Exception {
        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(document, new FileOutputStream(fichier));
    }
}